"""
To use, either use Plone 2.0+ to install, or add an external 
method to the root of the Plone Site that you want the skin registered 
in with the configuration:

 id: install_skin
 title: Install Skin *optional*
 module name: sdotNews.Install
 function name: install

Then go to the management screen for the newly added external method
and click the 'Try it' tab.  
"""
from Products.Archetypes.public import listTypes
from Products.Archetypes.Extensions.utils import installTypes, install_subskin
from Products.sdotNews.config import *
from Products.CMFCore.utils import getToolByName
from Products.CMFCore import permissions
from StringIO import StringIO

# Configlet to manage sdotNews
prefs_sdotNews_configlet = {
    'id': CONFIGLET_ID,
    'appId': PROJECTNAME,
    'name': 'sdotNews configuration',
    'action': 'string:$portal_url/prefs_sdotNews_form',
    'category': 'Products',
    'permission': (permissions.ManagePortal, ),
    'imageUrl': CONFIGLET_ICON,
    'visible'    : 1,
    }    

ACTION_ICONS = {'plone':
                {'sdotrssfeed':('sdotrss.png','RSS feed'),
                 }
                }
                

def install_properties(self, out):
    """ Install the properties of sdotNews
    they can be accessed with the configlet"""
    if not hasattr(self.portal_properties, 'sdotNews_properties'):
        self.portal_properties.addPropertySheet('sdotNews_properties', 'sdotNews properties')
    props = self.portal_properties.sdotNews_properties

    sdotNews_properties = (
        ('showIconsInNews', 'boolean', 1),
        ('typesToDisplay', 'lines', ['News Item','PloneArticle', 'PressClip' ]),
        ('typesToAdd', 'lines', ['News Item', ]),
        ('defaultAddNewsAction', 'string','folder_factories'),
        ('newsPageSize', 'int',15),
        ('newsRssSize', 'int',7),
        ('newsPortletSize', 'int',4),
        ('iconsInPortlet', 'boolean',1),
        ('titleInPortlet', 'boolean', 0),
        ('forumNewsPageSize', 'int',15),
        ('forumTypesToDisplay', 'lines', ['ForumTopic',  'ForumMessage']),
        ('forumStatesToDisplay', 'lines', ['published','open','public1','public2']),
        ('forumDefaultSort', 'string','modified'),
        )
   
    for prop_id, prop_type, prop_value in sdotNews_properties:
        if not hasattr(props, prop_id):
            props._setProperty(prop_id, prop_value, prop_type)

        
def install_configlet(self,out):
    """install the sdotNews configlet"""
    configTool = getToolByName(self, 'portal_controlpanel', None)
    if configTool:
        configTool.registerConfiglet(**prefs_sdotNews_configlet)
        out.write('Added configlet %s\n' % prefs_sdotNews_configlet['id'])

def install_actions(self, out):
    ai = getToolByName(self, 'portal_actionicons')
    for category, config in ACTION_ICONS.items():
        for icon_id, info in config.items():
            if ai.queryActionIcon(category, icon_id, None) is None:
                ai.addActionIcon(category, icon_id,
                                 info[0], info[1])
                out.write("Installed action icon "
                          "for %s.\n" % info[1])
            else:
                out.write(("Action Icon for %s "
                           "was already Installed.\n"
                           % info[1]))


def install_slot(self,path,name,out):
    """ Install one slot.
    """
    done=0
    #left_slots  = list(getattr(self,'left_slots'))
    #right_slots = list(getattr(self,'right_slots'))
    if path not in left_slots and path not in right_slots:
        done=1
        right_slots.append(path)
        self._updateProperty('right_slots',right_slots)
        out.write('%s successfully installed\n' %(name))
    if done==0:
        out.write('%s (%s) already exists\n' %(name,path))
        
def install_slots(self,out):
    """ Install Portlet.
    """
    #install_slot(self,'here/portlet_newscategories/macros/portlet','News categories portlet',out)
 
def install(self):
    out = StringIO()
    
    installTypes(self, out,
                 listTypes(PROJECTNAME),
                 PROJECTNAME)

    install_subskin(self, out, GLOBALS)
    #install_slots(self,out)
    install_actions(self, out)
    install_properties(self,out)
    install_configlet(self,out)
    ##
    ## Register sdotNews as folderish content in plone, so it will
    ## get the appropriate folder_contents tabs
    props = self.portal_properties.site_properties
    tabs = getattr(props, 'use_folder_tabs')
    contents = getattr(props, 'use_folder_contents')

    if 'sdotNewsFolder' not in tabs:
        newtabs = list(tabs)
        newtabs.append('sdotNewsFolder')
        props._updateProperty('use_folder_tabs', newtabs)

    if 'sdotNewsFolder' not in contents:
        newcontents = list(contents)
        newcontents.append('sdotNewsFolder')
        props._updateProperty('use_folder_contents', newcontents)
    
    print >> out, "Successfully installed %s." % PROJECTNAME
    return out.getvalue()
    
def uninstall(self):
    out = StringIO()

    # remove the configlets from the portal control panel
    configTool = getToolByName(self, 'portal_controlpanel', None)
    if configTool:
        configTool.unregisterConfiglet(prefs_sdotNews_configlet['id'])
        out.write('Removed configlet %s\n' % prefs_sdotNews_configlet['id'])

    print >> out, "Successfully uninstalled %s." % PROJECTNAME
    return out.getvalue()
